// pages/removeIPay/removeIPay.js

var util = require('../../utils/util.js');

const app = getApp()

Page({
  data: {
    userInfo: app.globalData.userInfo,
    animationData: {},
    cardInfoList: [],
    jlr: "",
    jlr_items: [

    ],
    ifInit: false,
    sz_items: [{
        name: '-',
        value: '支出',
        checked: 'true'
      },
      {
        name: '+',
        value: '收入'
      }
    ],
    shouzhi: '-',
  },

  //类型单选
  sz_radioChange: function (e) {
    this.setData({
      shouzhi: e.detail.value
    });
    console.log(this.data.shouzhi);
    console.log('radio发生change事件，携带value值为：', e.detail.value);

    var that = this;
    if (that.init()) {
      that.showDiary();
    }
  },


  showDiary: function () {
    var postObj = {
      "start_time": util.formatDate(new Date(Date.parse(new Date()) - 30 * 24 * 60 * 60 * 1000)),
      "end_time": util.formatDate(new Date()),
      "creator_id": "\"" + wx.getStorageSync("openid") + "\"",
      "in_out": this.data.shouzhi,
    };

    var items = [];

    wx.request({
      url: wx.getStorageSync("sURL"),
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      data: {
        url: '/getExpQuery',
        postdata: JSON.stringify(postObj)
      },
      method: 'POST',
      success: res => {
        console.log(res);
        var str = res.data.result;
        var str2 = JSON.parse(str);
        var rw = str2["rows"];
        //轮询获得微信账号名称
        rw.forEach(function (item, index) {
          var data = {};
          data.cardTitle = item["CONTENT"];
          data.yearDay = item["PRICE"];
          data.totalDay = item["CONTENT"];
          data.recordTime = item["RECORD_TIME1"];
          data.creator = item["CREATOR"]
          data.id = item["ID"];
          data.lunar = item["CATEGORY"];
          items.push(data);
        });
        this.setData({
          cardInfoList: items
        });
      }
    });
  },

  //事件处理函数
  slidethis: function (e) {
    console.log(e);
    var animation = wx.createAnimation({
      duration: 300,
      timingFunction: 'cubic-bezier(.8,.2,.1,0.8)',
    });
    var self = this;
    this.animation = animation;
    this.animation.translateY(-420).rotate(-5).translateX(0).step();
    this.animation.translateY(62).translateX(25).rotate(0).step();
    this.setData({
      animationData: this.animation.export()
    });
    setTimeout(function () {
      var cardInfoList = self.data.cardInfoList;
      var slidethis = self.data.cardInfoList.shift();
      self.data.cardInfoList.push(slidethis);
      self.setData({
        cardInfoList: self.data.cardInfoList,
        animationData: {}
      });
    }, 350);
  },

  init: function () {
    var that = this;

    if (that.data.ifInit)
      return true;

    that.setData({
      // userInfo: wx.getStorageSync("userInfo")
      userInfo: app.globalData.userInfo
    });

    if (that.data.userInfo == undefined || that.data.userInfo == {} || that.data.userInfo == "") {
      wx.showToast({
        title: '请先在主页点击授权',
        icon: 'none',
        duration: 3000
      });
    } else {
      that.setData({
        ifInit: true
      });
      return true;
    }
    return false;
  },


  getGroupMember: function () {
    var postObj = {
      "OPENID": "\"" + wx.getStorageSync("openid") + "\""
    };

    wx.request({
      url: wx.getStorageSync("sURL"),
      header: {
        'content-type': 'application/x-www-form-urlencoded' // 默认值
      },
      data: {
        url: '/webUserView',
        postdata: JSON.stringify(postObj)
      },
      method: 'POST',
      success: res => {
        console.log(res);
        var str = res.data.result;
        var str2 = JSON.parse(str);
        var rw = str2["rows"];

        var group = rw[0]["DIARY_GROUP"];
        var members = (group || '').split(",");
        var inStr = '';
        //初始化,DIARY_GROUP里是包含自己的
        var items = [];

        members.forEach(function (item, index) {

          inStr += "\"" + item + "\"";
          if (index != members.length && index != 1) {
            inStr += ','
          }
        });
        if (inStr != '') {
          var postObj2 = {
            "OPENID": inStr
          };
          wx.request({
            url: wx.getStorageSync("sURL"),
            header: {
              'content-type': 'application/x-www-form-urlencoded' // 默认值
            },
            data: {
              url: '/webUserView',
              postdata: JSON.stringify(postObj2)
            },
            method: 'POST',
            success: res => {
              var str3 = JSON.parse(res.data.result);
              var rw3 = str3["rows"];

              //轮询获得微信账号名称
              rw3.forEach(function (item, index) {
                var self3 = {};
                self3.name = item["OPENID"];
                self3.value = item["NICKNAME"];
                self3.checked = true;
                items.push(self3);
              });

              this.setData({
                jlr_items: items,
                jlr: inStr
              });
            }
          });
        }
      }
    })
  },

  addIPayBtn: function (options) {
    wx.navigateTo({
      url: '../addINote/addINote'
    })
  },

  delete: function (e) {
    var that = this;
    var id = e.currentTarget.dataset.name;
    console.log(id);
    var postObj = {
      "ID": id,
    };

    wx.showModal({
      title: '提示',
      content: '是否确认删除？',
      success(res) {
        if (res.confirm) {
          wx.request({
            url: wx.getStorageSync("sURL"),
            header: {
              'content-type': 'application/x-www-form-urlencoded' // 默认值
            },
            data: {
              url: '/deleteExp',
              postdata: JSON.stringify(postObj)
            },
            method: 'POST',
            success: res => {
              console.log('成功：', res);
              that.onShow();
            }
          });
        }
      }
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // var that = this;
    // if(that.init()) {
    // that.getGroupMember();
    // that.showDiary();
    // }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    if (that.init()) {
      // if(that.data.jlr_items == undefined || that.data.jlr_items.length == 0){
      //   that.getGroupMember();
      // }
      that.showDiary();
      // setTimeout(function () {
      //   that.showDiary();
      //  }, 500) 
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})